home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Encoder / •Sources / Color Search Procs.cp next >
Encoding:
Text File  |  1996-08-15  |  3.8 KB  |  147 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    Color Search Procs.cp
  4. #    
  5. #    This file defines a number of commonly used color search procs.  For the encoder,
  6. #   we only use one of the procs, which returns white if the color of a pixel is within
  7. #   a certain delta of a key color, and returns black for all other cases.  We can use
  8. #   this to quickly create masks of an image.
  9. #   
  10. #
  11. #    Author: Timothy Carroll
  12. #    Apple Developer Technical Support
  13. #    timc@apple.com
  14. #
  15. #    Modification History: 
  16. #
  17. #    8/15/96        TMC     Initial Release
  18. #
  19. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  20. #
  21. #
  22. #    You may incorporate this sample code into your applications without
  23. #    restriction, though the sample code has been provided "AS IS" and the
  24. #    responsibility for its operation is 100% yours.  However, what you are
  25. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  26. #    after having made changes. If you're going to re-distribute the source,
  27. #    we require that you make it clear in the source that the code was
  28. #    descended from Apple Sample Code, but that you've made changes.
  29. #
  30. *************************************************************************************/
  31.  
  32.  
  33.  
  34. #include "Color Search Procs.h"
  35.  
  36. SInt32        gSearchDelta = 0x2000;
  37. RGBColor    gMaskColor = {0,0,0};
  38.  
  39. // We're going to define a custom color search proc.  This will be used by the class routines
  40. // to create the mask.
  41.  
  42. static pascal Boolean    MaskSearchProc (RGBColor *theColor, long *result);
  43. static pascal Boolean    LightenSearchProc (RGBColor *theColor, long *result);
  44. static pascal Boolean   DarkenSearchProc (RGBColor *theColor, long *result);
  45.  
  46. // We want to be able to compile for powermacs, so we'll provide UPPs for all
  47. // of the custom search procs. Note that we don't actually allow passing
  48. // the search proc's directly, only the UPPs.
  49. ColorSearchUPP MaskSearchProcUPP = NewColorSearchProc(MaskSearchProc);
  50. ColorSearchUPP LightenSearchProcUPP = NewColorSearchProc(LightenSearchProc);
  51. ColorSearchUPP DarkenSearchProcUPP = NewColorSearchProc(DarkenSearchProc);
  52.  
  53.  
  54. // The Mask search proc needs to be able to pass in a specific color to be used as
  55. // the mask.  We can't change the calling parameters or provide it via a handle
  56. // (unlike some System 7 routines, this one doesn't pass any "user data".). 
  57. // So any additional parameters, like our key color and the delta, need to be
  58. // defined and passed in as global variables.
  59.  
  60.  
  61. pascal Boolean MaskSearchProc (RGBColor *theColor, long *result)
  62. {
  63.     long temp;
  64.     if (theColor->red > gMaskColor.red)
  65.     {
  66.         temp = theColor->red - gMaskColor.red;
  67.         if (temp > gSearchDelta)
  68.         {
  69.             *theColor = kBlack;
  70.             return false;
  71.         }
  72.     }
  73.     else
  74.     {
  75.         temp = gMaskColor.red - theColor->red;
  76.         if (temp > gSearchDelta)
  77.         {
  78.             *theColor = kBlack;
  79.             return false;        
  80.         }
  81.     }
  82.     
  83.     if (theColor->green > gMaskColor.green)
  84.     {
  85.         temp = theColor->green - gMaskColor.green;
  86.         if (temp > gSearchDelta)
  87.         {
  88.             *theColor = kBlack;
  89.             return false;
  90.         }
  91.     }
  92.     else
  93.     {
  94.         temp = gMaskColor.green - theColor->green;
  95.         if (temp > gSearchDelta)
  96.         {
  97.             *theColor = kBlack;
  98.             return false;        
  99.         }
  100.     }
  101.     
  102.     if (theColor->blue > gMaskColor.blue)
  103.     {
  104.         temp = theColor->blue - gMaskColor.blue;
  105.         if (temp > gSearchDelta)
  106.         {
  107.             *theColor = kBlack;
  108.             return false;
  109.         }
  110.     }
  111.     else
  112.     {
  113.         temp = gMaskColor.blue - theColor->blue;
  114.         if (temp > gSearchDelta)
  115.         {
  116.             *theColor = kBlack;
  117.             return false;        
  118.         }
  119.     }
  120.     
  121.     *theColor = kWhite;
  122.     return false;
  123.  
  124. #pragma unused (result)
  125. }
  126.  
  127.  
  128. pascal Boolean DarkenSearchProc (RGBColor *theColor, long *result)
  129. {
  130.     (theColor->red) >>= 1;
  131.     (theColor->green) >>= 1;
  132.     (theColor->blue) >>= 1;
  133.     
  134.     return false;
  135. #pragma unused (result)
  136. }
  137.  
  138.  
  139. pascal Boolean LightenSearchProc (RGBColor *theColor, long *result)
  140. {
  141.     (theColor->red) <<= 1;
  142.     (theColor->green) <<= 1;
  143.     (theColor->blue) <<= 1;
  144.  
  145.     return false;
  146. #pragma unused (result)
  147. }